package ru.ifmo.itmokis.util;

import java.util.Random;
import ru.ifmo.itmokis.domain.Time;

/**
 *
 * @author Ivan Stepuk
 */
public class Randoms {

    private Random random;

    public Randoms() {
        random = new Random();
    }

    public Randoms(long seed) {
        random = new Random(seed);
    }

    public boolean probableBoolean(double probabilityOfTrue) {
        return random.nextDouble() < probabilityOfTrue;
    }

    public int gaussianPositiveInt(int mean, int deviation) {
        double result = mean + random.nextGaussian() * deviation;
        if (result > 0) {
            return (int) Math.round(result);
        }
        return 0;
    }

    public Time gaussianTime(Time mean, Time deviation) {
        double result = mean.toPlainHours()
                + random.nextGaussian() * deviation.toPlainHours();
        if (result > 0) {
            return new Time((int) Math.round(result));
        }
        return Time.ONE_HOUR;
    }

    public Time gammaTime(Time time, int scale) {
        // M = scale * time
        // D = scale * time^2
        double result = 1.0;
        for (int i = 0; i < scale; i++) {
            result *= random.nextDouble();
        }
        if (result > 0) {
            return new Time((int) Math.round(-time.toPlainHours() * Math.log(result)));
        }
        return Time.ONE_HOUR;
    }
}
